<?php
/**
 * Hellos Model for Hello World Component
 * 
 * @package    Joomla.Tutorials
 * @subpackage Components
 * @link http://docs.joomla.org/Developing_a_Model-View-Controller_Component_-_Part_4
 * @license		GNU/GPL
 */

// No direct access
defined( '_JEXEC' ) or die( 'Restricted access' );

jimport( 'joomla.application.component.model' );

/**
 * Hello Model
 *
 * @package    Joomla.Tutorials
 * @subpackage Components
 */
class TargetsModelDetails1 extends JModel
{
	/**
	 * Hellos data array
	 *
	 * @var array
	 */
	var $_data;
	var $type_id;
	var $target_id;
	var $_header;
	var $_header2;
	
	function __construct() {
		$this->type_id   = JRequest::getInt( 'type_id');
		$this->target_id = JRequest::getInt( 'target_id');
		
		parent::__construct();
	}


	/**
	 * Returns the query
	 * @return string The query to be used to retrieve the rows from the database
	 */
	function _buildQuery()
	{
		#$type_id   = JRequest::getInt( 'type_id');
		#$target_id = JRequest::getInt( 'target_id');
				
		#if (!preg_match('/^\d+$/', $type_id)) {
		#	$type_id = 1;
		#}
		
		#$f = fopen('/tmp/test_var.txt', 'w+');
		#fwrite($f, 'type_id = ' . $type_id);
		#fclose($f);
		
		
		$query = ' SELECT id, detail_name, detail_description, created '
			. " FROM #__trg_details1 where type_id = {$this->type_id} and target_id = {$this->target_id}";

		return $query;
	}

	/**
	 * Retrieves the hello data
	 * @return array Array of objects containing the data from the database
	 */
	function getData()
	{
		// Lets load the data if it doesn't already exist
		if (empty( $this->_data ))
		{
			$query = $this->_buildQuery();
			$this->_data = $this->_getList( $query );
		}

		return $this->_data;
	}
	
	function _getHeaders() {
		$query = "select type_name, type_name_single from #__trg_details1_types where type_id = {$this->type_id}";
		$data = $this->_getList($query);
		
		$this->_header  = $data[0]->type_name;
		$this->_header2 = $data[0]->type_name_single;
	}
	
	function getHeader() {
		if (empty ($this->_header)) {		
			$this->_getHeaders();
		}
		return $this->_header;
	}
	
	function getHeader2() {
		if (empty ($this->_header2)) {		
			$this->_getHeaders();
		}
		return $this->_header2;
	}
}